﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admins
{
    public partial class Contract : System.Web.UI.Page
    {
        static string Id = "";
        static bool Insert = false;
        static string Lang = "";
        static string userID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                Lang = Session["Lang"].ToString();
                BindGrid();
            }
        }

        private void BindGrid()
        {
            if (Request.QueryString["ID"] != null && Session["Admin"].Equals("1"))
            {
                userID = Request.QueryString["ID"];
                grdContract.DataSource = ContractService.Contract_GetByTop("", "uID=" + userID, "");
                grdContract.DataBind();
            if (grdContract.PageCount <= 1)
            {
                grdContract.PagerStyle.Visible = false;
            }
            }
            else
            {
                pnView.Visible = false;
                List<Data.Contract> listC = new List<Data.Contract>();
                string level = UsersService.Users_GetById(Session["uID"].ToString())[0].Level;
                List<Data.Users> li = new List<Data.Users>();
                li = UsersService.Users_GetByAll();
                foreach (Data.Users u in li)
                {
                    if (u.Level.Substring(0, level.Length) == level)
                    {
                        listC.AddRange(ContractService.Contract_GetByTop("","uID="+u.uID,""));
                    }
                }
                grdContract.DataSource = listC;
                grdContract.DataBind();
                if (grdContract.PageCount <= 1)
                {
                    grdContract.PagerStyle.Visible = false;
                }
            }
            
        }

        protected void grdContract_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdContract.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
                    }
                    Label lblBatDau = e.Item.FindControl("lblBatDau") as Label;
                    Label lblKetThuc = e.Item.FindControl("lblKetThuc") as Label;
                    Label lblNgayDang = e.Item.FindControl("lblNgayDang") as Label;
                    lblBatDau.Text = DateTimeClass.ConvertDateTime(((Data.Contract)e.Item.DataItem).StartDate);
                    lblKetThuc.Text = DateTimeClass.ConvertDateTime(((Data.Contract)e.Item.DataItem).EndDate);
                    lblNgayDang.Text = DateTimeClass.ConvertDateTime(((Data.Contract)e.Item.DataItem).Date);
                }
            }
        }

        protected void grdContract_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdContract.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdContract_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    Id = strCA;
                    List<Data.Contract> listE = ContractService.Contract_GetById(Id);
                    txtNumber.Text = listE[0].Number;
                    txtType.Text = listE[0].Type;
                    txtStartDate.Text = DateTimeClass.ConvertDateTime(listE[0].StartDate);
                    txtEndDate.Text = DateTimeClass.ConvertDateTime(listE[0].EndDate);
                    txtDate.Text = DateTimeClass.ConvertDateTime(listE[0].Date);
                    fckDetail.Value = listE[0].Detail;
                    txtStatus.Text = listE[0].Status;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;
                case "Delete":
                    ContractService.Contract_Delete(strCA);
                    BindGrid();
                    break;
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            txtStartDate.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
            txtEndDate.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
            txtDate.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
            pnView.Visible = false;
            Insert = true;
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            DataGridItem item = default(DataGridItem);
            for (int i = 0; i < grdContract.Items.Count; i++)
            {
                item = grdContract.Items[i];
                if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                {
                    if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                    {
                        string strId = item.Cells[1].Text;
                        ContractService.Contract_Delete(strId);
                    }
                }
            }
            grdContract.CurrentPageIndex = 0;
            BindGrid();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Data.Contract obj = new Data.Contract();
                obj.ConID = Id;
                obj.uID = userID;
                obj.Number = txtNumber.Text;
                obj.Type = txtType.Text;
                obj.StartDate = DateTimeClass.ConvertDateTime(txtStartDate.Text, "MM/dd/yyyy HH:mm:ss");
                obj.EndDate = DateTimeClass.ConvertDateTime(txtEndDate.Text, "MM/dd/yyyy HH:mm:ss");
                obj.Date = DateTimeClass.ConvertDateTime(txtDate.Text, "MM/dd/yyyy HH:mm:ss");
                obj.Detail = fckDetail.Value;
                obj.Status = txtStatus.Text;
                if (Insert == true)
                {
                    ContractService.Contract_Insert(obj);
                }
                else
                {
                    ContractService.Contract_Update(obj);
                }
                BindGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }


       
    }
}